// @ts-nocheck
import { createRouter, createWebHistory } from 'vue-router'
import {useUserStore} from "@/stores/userStore";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: () => import((`@/components/views/Root.vue`)),
      meta: { requiresAuth: false }
    },
    {
      path:'/Login_Register',
      name: 'Login_Register',
      component: () => import((`@/components/views/Login_Register.vue`)),
      meta: { requiresAuth: false }
    },
    {
      path:'/Homepage',
      name:'Homepage',
      component: () => import((`@/components/views/Homepage.vue`)),
      meta: { requiresAuth: true },
      children: [
        {
          path:'',
          name:'Personal_data',
          component: () => import((`@/components/views/Homepage/Personal_data.vue`)),
        },
        {
          path:'/Homepage/settings',
          name:'Settings',
          component: () => import((`@/components/views/Homepage/Settings.vue`)),
        }
      ]
    },
    {
      path: '/CTF',
      name: 'CTF',
      component: () => import((`@/components/views/CTF_topic.vue`)),
      children: [
        {
          path: '',
          name: 'ctf_web',
          component: () => import((`@/components/views/CTF_topic/CTF_web.vue`)),
        },
        {
          path: '/CTF/pwn',
          name: 'ctf_pwn',
          component: () => import((`@/components/views/CTF_topic/pwn.vue`)),
        },
        {
          path: '/CTF/crypto',
          name: 'ctf_crypto',
          component: () => import((`@/components/views/CTF_topic/crypto.vue`)),
        },
        {
          path: '/CTF/misc',
          name: 'ctf_misc',
          component: () => import((`@/components/views/CTF_topic/misc.vue`)),
        },
      ]
    },
    {
      path: '/WriteUp',
      name: 'WriteUp',
      component: () => import((`@/components/views/WriteUp.vue`)),
      children: [
        {
          path: '',
          name: 'wp_index',
          component: () => import((`@/components/views/WriteUp/WP_Index.vue`)),
        },
        {
          path: '/WriteUp/web/:filename',
          name: 'wp_web',
          component: () => import((`@/components/views/WriteUp/WP_Web.vue`)),
        },
        {
          path: '/WriteUp/pwn/:filename',
          name: 'wp_pwn',
          component: () => import((`@/components/views/WriteUp/WP_Pwn.vue`)),
        },
        {
          path: '/WriteUp/crypto/:filename',
          name: 'wp_crypto',
          component: () => import((`@/components/views/WriteUp/WP_Crypto.vue`)),
        },
        {
          path: '/WriteUp/misc/:filename',
          name: 'wp_misc',
          component: () => import((`@/components/views/WriteUp/WP_Misc.vue`)),
        },
      ]
    },
    {
      path:'/manage_CTF',
      name: 'manage_ctf',
      component: () => import((`@/components/views/Manage_CTF.vue`)),
    }
  ]
})

// 全局前置守卫
router.beforeEach((to, _from, next) => {
  const userStore = useUserStore();
  // 如果路由需要认证，但用户没有访问令牌，则重定向到登录页面
  if (to.meta.requiresAuth && !userStore.access_token) {
    next('/Login_Register');
  } else if (!userStore.access_token && to.path !== '/' && to.path !== '/Login_Register') {
    // 如果用户没有访问令牌，并且试图访问非主页或登录注册页面的路由，则重定向到主页{
    next('/');
  }else {
    // 如果用户已经登录，或者访问的是非认证路由，则允许访问
    next();
  }
});

export default router
